<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@page import="com.data.*" %>
<%@page import="com.yewu.*" %>
<%@page import="java.util.*" %>
<% 
	boolean isLogin =false;
	if(request.getSession().getAttribute("Login")!=null){
		isLogin = true;
	}

	
	int  totalNum=0;
	int  presentPage =0;//点翻页前的页码
	int  start = 0;//开始显示位置
	int  end =0;//页面显示数量限制
	int  fp=0;//记录当前输出的是第多少个结果
	boolean flag=false;//是否尾页标志
	String contextPath =request.getContextPath();
	String s=(String)request.getParameter("presentPage");
	if(s==null){
		presentPage = 0;
	}else{
		presentPage=Integer.parseInt(s);
		if(presentPage<=0){
			presentPage = 0;
		}
	}
	start = presentPage*15;//当前输出的开始处
	end = start+15;
	
	
	
	String nowAccount = (String)request.getSession().getAttribute("user");//当前登录者账号
	 
%>
<input type = "hidden" id = "contextPath" value =<%=contextPath %>/>
<%
	String postNum = request.getParameter("post_Num");//获取到帖子编号
	YeWu yeWu = new YeWu(postNum);
	yeWu.ConnectDB();
	Post post = yeWu.getPost();
	
	
	
	if(post==null){
%>
	<jsp:forward page="/tiezi/error.jsp"></jsp:forward>
	<%	//response.sendRedirect("/tiezi/error.jsp");//为何这两个不可以
		//request.getRequestDispatcher("/tiezi/error.jsp").forward(request, response);
	}
	%>

	<!-- 主贴的信息输出部分，主贴的点赞提交到PostServlet处理 -->
	<!-- 上诉方案不可行，换用js方案实现，所以所有的赞暂时换为button，响应js -->
	<div style="padding:10px;padding-bottom:0px;">
		<a href="homepage2.jsp?account=<%if(yeWu.userInfo1.get("this")!=null){out.println(yeWu.userInfo1.get("this").account);} %>">
		<%if(yeWu.userInfo1.get("this")!=null){out.println(yeWu.userInfo1.get("this").nickname);} %></a>:
		<br />
		
		<div>&nbsp;&nbsp;&nbsp;&nbsp;【<%=post.theme %>】<%=post.content %></div>
		<br /><div align="right">时间:<%=post.time %></div>
		<div id ='thegoodorbadofpost'>
		
		
		
		<%if(isLogin){
			int type=yeWu.isZan(nowAccount, Integer.parseInt(postNum));//当前贴被赞状态
			System.out.println("type="+type);
			if(type==0){//未评论过
				
			
		%>
			
			
			<input type="button" value='赞一个 ' onclick="goodPost('<%=nowAccount %>','<%=post.postNum %>')" />
			<input type="button" value='丢块砖' onclick="badPost('<%=nowAccount %>','<%=post.postNum %>')"/>
			<a href="#direct"><input type="button" value='回复' onclick=""/></a>
		<%	}else{
				if(type==1){//已经点赞了，就显示丢块砖
		%>
			<input type="button" value='丢块砖' onclick="badPost('<%=nowAccount %>','<%=post.postNum %>')"/>
		<%			
				}else{
		%>
			<input type="button" value='赞一个 ' onclick="goodPost('<%=nowAccount %>','<%=post.postNum %>')" />
		<%			
				}
		%>
		
				<a href="#direct"><input type="button" value='回复' onclick=""/></a>
		<%	
			}//type==0
			//回复的链接，只要登录就显示的
		%>
			
		<%
		  }//login 
		%>
		
		
		
		
		
		</div>
	</div>
	<hr />
	<!-- 回帖的信息输出部分 -->
<% 
	List<Direct> directSet =yeWu.getDirects();	//获取到所有回复
	List<Indirect> indirectSet;					//评论集合
	Direct direct;								//当前回复
	Indirect indirect;							//当前回复的所有子评论
	
	
	totalNum=directSet.size();//直接回帖数量
	for(int i=0;i<directSet.size();i++){		//循环处理每一个回复
		direct=directSet.get(i);				//获取一个回复对象
		if(fp>=start&&fp<end){//当前指针到达可输出位置才输出
%>

<hr />
	<!-- 回复信息的输出 ,对于一条回复信息,作为表单输出,提交到DirectServlet处理-->
<form action="<%=contextPath %>/DirectServlet?post_Num=<%=postNum %>" method="post">					
	<input type="hidden" name="directNum" value ='<%=direct.directNum %>'>
	<div style="padding:5px;padding-right:0px;">
	<div>
		<!-- <tr><td>回复者账户:<%=direct.account %></td></tr> -->
		<div>
			<a href="homepage2.jsp?account=<%if(yeWu.userInfo1.get(direct.directNum+"")!=null){out.println(yeWu.userInfo1.get(direct.directNum+"").account);} %>"><%if(yeWu.userInfo1.get(direct.directNum+"")!=null){out.println(yeWu.userInfo1.get(direct.directNum+"").nickname);} %></a>:
		</div>
		<div>
			<%=direct.content %>  <!-- @<%if(yeWu.userInfo1.get("this")!=null){out.println(yeWu.userInfo1.get("this").nickname);}  %> -->
		</div>
		<div align="right"><%=direct.time %></div>
		<div id ='<%=direct.directNum %>'><!-- 更换回复的赞的id -->
		
		
		
		<%if(isLogin){
			int thisDirectNum =direct.directNum;
			thisDirectNum=-thisDirectNum;//回复在good_or_bad表中编号存为负值
			int type=yeWu.isZan(nowAccount, thisDirectNum);//当前回复被赞状态
			System.out.println("type="+type);
			if(type==0){//未评论过
				
			
		%>
			
			
			<input type="button" value='赞一个 ' onclick="goodDirect('<%=nowAccount %>','<%=thisDirectNum %>')" />
			<input type="button" value='丢块砖' onclick="badDirect('<%=nowAccount %>','<%=thisDirectNum %>')"/>
			<input type="button" value='回复 ' onclick="comment('<%="direct"+direct.directNum %>')"/>
		<%	}else{
				if(type==1){//已经点赞了，就显示丢块砖
		%>
			<input type="button" value='丢块砖' onclick="badDirect('<%=nowAccount %>','<%=direct.directNum %>')"/>
		<%			
				}else{
		%>
			<input type="button" value='赞一个 ' onclick="goodDirect('<%=nowAccount %>','<%=direct.directNum %>')" />
		<%			
				}
		%>
		
			<input type="button" value='回复 ' onclick="comment('<%="direct"+direct.directNum %>')"/>
		<%	
			}//type==0
			//回复的链接，只要登录就显示的
		%>
			
		<%
		  }//login 
		%>
		
		
		
		
		
		</div>
		<div id ='<%="direct"+direct.directNum %>'></div><!-- 被替换为输入的位置 -->
	</div>
	</div>
</form>
<hr />
	<!-- 对于每一个回复输出全部子评论 -->
	<%	}//if判断回复是否需要输出的
		fp++;//当前指针后移
		//int directNum = direct.directNum;			//获取当前回复的编号
		indirectSet=yeWu.getIndirects(direct.directNum);	//获取当前回复的所有子评论
		totalNum+=indirectSet.size();//每个直接回帖的评论量
		for(int j=0;j<indirectSet.size();j++){		//循环输出全部子评论
			indirect = indirectSet.get(j);
			if(fp>=start&&fp<end){//当前指针到达可输出位置才输出
	%>
	
	<!-- 对于每一个评论，以表单形式输出到页面 ，点击回复评论时提交到IndirectServlet处理-->		
<form action="<%=contextPath %>/IndirectServlet?post_Num=<%=postNum %>" method="post">
	<input type="hidden" name="indirectNum" value ='<%=indirect.indirectNum %>'>
			<div style="padding-left:40px;border-width:0px;border-top-width:20px;border-top-color:white;border-style:dashed;border-bottom-width:1px;">
		    	<!-- <tr><td>&nbsp;&nbsp;&nbsp;&nbsp;评论者账户:<%=indirect.account %></td></tr> -->
		    	<div><!-- 这里添加空格 -->
		    		<a href="homepage2.jsp?account=<%if(yeWu.userInfo2.get(indirect.indirectNum+"")!=null){out.println(yeWu.userInfo2.get(indirect.indirectNum+"").account);} %>"><%if(yeWu.userInfo2.get(indirect.indirectNum+"")!=null){out.println(yeWu.userInfo2.get(indirect.indirectNum+"").nickname);} %></a>
		    		:<%=indirect.content%>
		    	<a href="homepage2.jsp?account=<%	if(indirect.level==1){
						if(yeWu.userInfo1.get(""+(-indirect.replyNum))!=null){
							out.println(yeWu.userInfo1.get(""+(-indirect.replyNum)).account);
						}
					}
					else{
						if(yeWu.userInfo2.get(indirect.replyNum+"")!=null){
							out.println(yeWu.userInfo2.get(indirect.replyNum+"").account);} 
						}
				
				%>
		    	
		    	
		    	
		    	
		    	">
		    	@<%	if(indirect.level==1){
						if(yeWu.userInfo1.get(""+(-indirect.replyNum))!=null){
							out.println(yeWu.userInfo1.get(""+(-indirect.replyNum)).nickname);
						}
					}
					else{
						if(yeWu.userInfo2.get(indirect.replyNum+"")!=null){
							out.println(yeWu.userInfo2.get(indirect.replyNum+"").nickname);} 
						}
				
				%>
				</a>
				</div>
				<div align = "right"><%=indirect.time	  %></div>
				<!-- <tr><td>&nbsp;&nbsp;&nbsp;&nbsp;评论的层次:<%=indirect.level  %></td></tr>  -->
				<div>
				<%if(isLogin){ %>
				<input type="button" value=回复 onclick="comment('<%="indirect"+indirect.indirectNum %>')"/>
				<%} %>
				</div>
				<div id ='<%="indirect"+indirect.indirectNum %>'></div><!-- 被替换为输入的位置 -->
			</div>
</form>
<%			}//
			fp++;
		}
	}
%>


<% yeWu.closeDB();//页面显示完毕后关闭数据库连接%>
<table style="width:100%">
<tr>
<td align = "center">
<%if(start>0){%>
<form action="tiezi.jsp" method="post">
	<input type = "hidden" name ="post_Num" value=<%=postNum %>>
	<input type = "hidden" name ="presentPage" value =<%=presentPage-1 %> >
	<input type = "submit" name ="submit" value = "上一页"  >
</form>
<%}%>
</td>
<td align = "center">
<%if(end<totalNum){%>
<form action="tiezi.jsp" method="post">
	<input type = "hidden" name ="post_Num" value=<%=postNum %>>
	<input type = "hidden" name ="presentPage" value =<%=presentPage+1 %> >
	<input type = "submit" name ="submit" value = "下一页"  >
</form>
<%}%>
</td>
</tr>
</table>